var phppath = "../php/";

function OpenDialog(callback) {
	var self = this;

	this.filename = "";
	this.win = null;

	// Folder view tree data
	this.tree_loader = new Ext.tree.TreeLoader({
		dataUrl: phppath + 'tree_loader.php'
	});

	// Root Node of tree
	this.root_node = new Ext.tree.AsyncTreeNode({
		text: 'Home',
		draggable: false,
		id: '0'
	});

	// Tree Panel
	this.tree_panel = new Ext.tree.TreePanel({
		root: this.root_node,
		region: 'west',
		title: 'Folders',
		width: 100,
		loader: this.tree_loader,
		containerScroll: true,
		split: true
	});

	// Grid Data
	this.grid_store = new Ext.data.SimpleStore({
		fields: [
			{ name: 'filename' },
			{ name: 'date_mod' },
			{ name: 'type' },
			{ name: 'path' }
		]
	});

	// Grid Panel
	this.grid_panel = new Ext.grid.GridPanel({
		store: this.grid_store,
		columns: [
			{ header: 'Name', width: 80 },
			{ header: 'Date Modified', width: 100 },
			{ header: 'Type', width: 80 },
			{ header: 'Path', width: 80 }
		],
		region: 'center'
	});

	this.grid_panel.addListener('rowclick', function(grid, rowIndex, e) {
		var record = grid.getStore().getAt(rowIndex);
		var data = record.get('path');
		var data2 = record.get('filename');
		var filefield = new Ext.form.TextField(Ext.get('filename'));
		var filepathfield = new Ext.form.TextField(Ext.get('filepath'));
		filepathfield.setValue(data);
		filefield.setValue(data2);
	});

	this.tree_panel.addListener('click', function(node, e) {
		Ext.Ajax.request({
			url: phppath + 'grid_loader.php?folder=' + node.text,
			success: function(response) {
				self.grid_store.loadData(eval(response.responseText));
			}
		});
	});

	this.panel = new Ext.Panel({
		layout: 'border',
		baseCls: 'x-plain',
		region: 'center',
		items: [ this.grid_panel, this.tree_panel	]
	});

	this.panel2 = new Ext.form.FormPanel({
		region: 'south',
		frame: true,
		height: 75,
		labelWidth: 55,
		bodyStyle: 'padding:5px 5px 0',
		defaultType: 'textfield',
		items: [
			{
				fieldLabel: 'Filename',
				name: 'filename',
				id: 'filename',
				anchor: '100%'
			},
			{
				xtype: 'hidden',
				name: 'filepath',
				id: 'filepath',
				anchor: '100%'
			},
			{
				fieldLabel: 'Filetype',
				name: 'filetype',
				value: 'All Files (*.*)',
				anchor: '100%'
			}
		]
	});

	this.show = function() {
		if(this.win == null) {
			this.win = new Ext.Window({
				title: 'Open Dialog',
				width: 450,
				height: 300,
				modal: true,
				layout: 'border',
				items: [ this.panel, this.panel2 ],
				buttons: [
					{
						text: 'Open',
						handler: function() {
							self.filename = Ext.get('filepath').getValue();
							callback(self.filename);
							self.win.close();
						}
					},
					{ text: 'Cancel', handler: function() { self.win.close(); } }
				]
			});
		}
		this.win.show();
	}

	this.getFilename = function() {
		return this.filename;
	}

	return true;
}

// Save Dialog
function SaveDialog(callback) {
	var self = this;

	this.filename = "";
	this.filepath = "";
	this.win = null;

	// Folder view tree data
	this.tree_loader = new Ext.tree.TreeLoader({
		dataUrl: phppath + 'tree_loader.php'
	});

	// Root Node of tree
	this.root_node = new Ext.tree.AsyncTreeNode({
		text: 'Home',
		draggable: false,
		id: '0'
	});

	// Tree Panel
	this.tree_panel = new Ext.tree.TreePanel({
		root: this.root_node,
		region: 'west',
		title: 'Folders',
		width: 100,
		loader: this.tree_loader,
		containerScroll: true,
		split: true
	});

	// Grid Data
	this.grid_store = new Ext.data.SimpleStore({
		fields: [
			{ name: 'filename' },
			{ name: 'date_mod' },
			{ name: 'type' },
			{ name: 'path' }
		]
	});

	// Grid Panel
	this.grid_panel = new Ext.grid.GridPanel({
		store: this.grid_store,
		columns: [
			{ header: 'Name', width: 80 },
			{ header: 'Date Modified', width: 100 },
			{ header: 'Type', width: 80 },
			{ header: 'Path', width: 80 }
		],
		region: 'center'
	});

	this.grid_panel.addListener('rowclick', function(grid, rowIndex, e) {
		var record = grid.getStore().getAt(rowIndex);
		var data = record.get('filename');
		var filefield = new Ext.form.TextField(Ext.get('filename'));
		filefield.setValue(data);
	});

	this.tree_panel.addListener('click', function(node, e) {
		var nodeval = node.text;
		Ext.Ajax.request({
			url: phppath + 'get_user.php',
			success: function(response) {
				self.filepath = response.responseText + nodeval;
			}
		});
		Ext.Ajax.request({
			url: phppath + 'grid_loader.php?folder=' + node.text,
			success: function(response) {
				self.grid_store.loadData(eval(response.responseText));
			}
		});
	});

	this.panel = new Ext.Panel({
		layout: 'border',
		baseCls: 'x-plain',
		region: 'center',
		items: [ this.grid_panel, this.tree_panel	]
	});

	this.panel2 = new Ext.form.FormPanel({
		region: 'south',
		frame: true,
		height: 75,
		labelWidth: 55,
		bodyStyle: 'padding:5px 5px 0',
		defaultType: 'textfield',
		items: [
			{
				fieldLabel: 'Filename',
				name: 'filename',
				id: 'filename',
				anchor: '100%'
			},
			{
				fieldLabel: 'Filetype',
				name: 'filetype',
				value: 'All Files (*.*)',
				anchor: '100%'
			}
		]
	});

	this.show = function() {
		if(this.win == null) {
			this.win = new Ext.Window({
				title: 'Save Dialog',
				width: 450,
				height: 300,
				modal: true,
				layout: 'border',
				items: [ this.panel, this.panel2 ],
				buttons: [
					{
						text: 'Save',
						handler: function() {
							self.filename = self.filepath + "/" + Ext.get('filename').getValue();
							callback(self.filename);
							self.win.close();
						}
					},
					{ text: 'Cancel', handler: function() { self.win.close(); } }
				]
			});
		}
		this.win.show();
	}

	this.getFilename = function() {
		return this.filename;
	}

	return true;
}
